<template>
  <div>
    <van-nav-bar title="转出到余额">
      <img
        src="~assets/image/arrow_left.png"
        alt=""
        slot="left"
        width="20"
        height="20"
        @click="leftClick"
      />
    </van-nav-bar>
    <div class="out">
      <div class="out-title">
        <img
          src="~assets/image/icon_wyf_hint.png"
          width="15"
          height="20"
          alt=""
        />
        <span>转出金额</span>
      </div>
      <van-cell-group>
        <van-field
          class="field"
          v-model="money"
          label="￥"
          clearable
          placeholder="请输入转出金额"
        >
        </van-field>
      </van-cell-group>
      <div class="out_count">
        <div>可转出金额 {{ accountInfo.rebateBalance | getMoney }} 元</div>
        <div class="out-right">
          <span @click="textClick">全部转出</span>
        </div>
      </div>
    </div>
    <p class="next" @click="next">确认转出</p>
    <p class="txt">
      注：转出到余额的资金，若提现至银行卡将收取提现服务费，请对资金合理安排
    </p>
  </div>
</template>
<script>
import {
  queryAccountInfoApi,
  executeTransferToBalanceApi,
} from "network/ApiServe";
export default {
  name: "TransferOut",
  filters: {
    getMoney(money) {
      return Number(money).toFixed(2);
    },
  },
  created() {
    this.token = localStorage.getItem("token");
    this.phone = localStorage.getItem("phone");
    this.brandId = localStorage.getItem("brandId");
  },
  data() {
    return {
      money: "",
      moneysum: 0.0,
      accountInfo: {},
    };
  },
  mounted() {
    this.getAccountInfo();
  },
  methods: {
    leftClick() {
      this.$router.back(-1);
    },
    next() {
      console.log("money:", this.money);
      if (this.money == "") {
        this.$toast("请输入转出金额");
        return;
      }
      if (Number(this.money).toFixed(2) < 1.0) {
        this.$toast("金额不能小于1元");
        return;
      }
      executeTransferToBalanceApi(
        this.phone,
        "分润提现",
        this.money,
        this.brandId
      )
        .then((res) => {
          if (res.resp_code === "000000") {
            this.$router.back(-1);
            this.$toast(res.resp_message);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getAccountInfo() {
      queryAccountInfoApi(this.token)
        .then((res) => {
          if (res.resp_code === "000000") {
            this.accountInfo = res.result;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    textClick() {
      this.money = this.accountInfo.rebateBalance;
    },
  },
};
</script>
<style scoped>
.out {
  background-color: #fff;
  padding: 20px;
}
.out-title {
  display: flex;
  align-items: center;
}
.out-title span {
  margin-left: 8px;
  font-size: 16px;
}
.van-cell-group {
  margin-top: 20px;
}
.out_count {
  display: flex;
  margin-top: 20px;
  justify-content: space-between;
  font-size: 15px;
}
.out-right {
  color: #f1ce8a;
}
.next {
  width: 90%;
  padding-top: 10px;
  padding-bottom: 10px;
  background-color: #121212;
  color: #f1ce8a;
  margin: 20px auto;
  text-align: center;
  border-radius: 4px;
  font-size: 16px;
}
.txt {
  margin-left: 20px;
  margin-right: 20px;
  font-size: 14px;
  line-height: 20px;
}
</style>